package com.usian.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.R;
import com.usian.mapper.EmpMapper;
import com.usian.pojo.Emp;
import com.usian.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    //查询
    @Override
    public Page<Emp> findPage(HttpSession session,Integer page, Integer pageSize, String name) {
        Integer id=(Integer) session.getAttribute("login");
        Emp emp=empMapper.findById(id);
        Integer code = emp.getGrade();
        Integer did = emp.getDid();

        Page<Emp> page1=new Page<>(page,pageSize);
        Page<Emp> page2=empMapper.findPage(page1,name,code,did,id);
        return page2;
    }

    //登录
    @Override
    public R<Emp> login(Emp emp) {
        //1.查询用户
        Emp login = empMapper.login(emp);
        if (login==null){
            return R.error("用户不存在！");
        }

        //2.校验密码
        if (!login.getPassword().equals(emp.getPassword())){
            return R.error("密码错误！");
        }

        if (login.getGrade()>=3){
            return R.error("该员工没有登录权限！");
        }

        return R.success(login);
    }

    //添加
    @Override
    public void add(Emp emp) {
        emp.setBirthday(LocalDate.now());
        emp.setEntryTime(LocalDate.now());
        emp.setPassword("123456");
        empMapper.insert(emp);
    }

    //回显
    @Override
    public R findById(Integer id) {
        Emp emp=empMapper.selectById(id);
        return R.success(emp);
    }

    //修改
    @Override
    public R edit(Emp emp) {
        empMapper.updateById(emp);
        return R.success("修改成功");
    }

    //删除
    @Override
    public R delete(String[] ids,HttpSession session) {
        Integer id=(Integer) session.getAttribute("login");
        for (String s : ids) {
            if (id==Integer.valueOf(s)){
                return R.error("选中对象含有已登录的员工，不能直接删除！");
            }
        }
        List<String> list = Arrays.asList(ids);
        empMapper.deleteBatchIds(list);
        return R.success(1);
    }
}
















